﻿/*******************************************************************************
#                    TroubleBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-8-21 13:08:14
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
#endregion

namespace ExpertMIS.Brokers
{
    public class TroubleBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateTroubleEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateTroubleEntity", entity);
        }

        private object InsertTroubleEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertTroubleEntity", entity);
        }

        private object DeleteTroubleEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteTroubleEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectTroubleEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            if (entity.EntityState == EntityState.New)
            {
                InsertTroubleEntity(entity);
                entity.EntityState = EntityState.Old;
            }
            else
            {
                UpdateTroubleEntity(entity);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            TroubleEntity entity = new TroubleEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteTroubleEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        /// <summary>
        /// 获取所有待审核困难列表
        /// </summary>
        /// <returns></returns>
        public IList<TroubleEntity> GetAllUnPassedTrouble()
        {
            return SqlMap.QueryForList<TroubleEntity>("SelectAllUnPassedTrouble", null);
        }

        /// <summary>
        /// 获取所有待审核困难条数
        /// </summary>
        /// <returns></returns>
        public int GetUnPassedTroublesCount()
        {
            return (int)SqlMap.QueryForObject("SelectUnPassedTroublesCount", null);
        }

        /// <summary>
        /// 通过用户名及联系电话查询困难信息
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="tel"></param>
        /// <returns></returns>
        public IList<TroubleEntity> GetTroublesByUserNameAndTel(string userName, string tel)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(tel))
            {
                return null;
            }

            Hashtable hsQuery = new Hashtable();
            hsQuery.Add("UserName", userName);
            hsQuery.Add("Tel", tel);
            return SqlMap.QueryForList<TroubleEntity>("SelectTroublesByUserNameAndTel", hsQuery);
        }
    }
}